/*##############################################################################

	Name	: ATTINY26 Motor driver - I2C via USI-TWI, TB6612FNG
	Version	: 0.1  - alpha
	autor	: Odo Maletzki odo@maletzki.net
	page	: www.maletzki.net
	License	: GNU General Public License 

	Created from Atmel source files for Application Note AVR312: 
	Using USI TWI Slave driver - I2C/TWI-EEPROM from Martin Junghans,jtronics@gmx.de
	and the rework 	16.11.2010 - reworked by Markus Schatzl
	
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details

//############################################################################*/

#ifndef _MOT_DRV_H_
#define _MOT_DRV_H_

#define SENSE_SPEED 0x04 // 0=off, 1=clk, 2=clk/8, 3=/64=488Hz, 4=/256=122Hz@8Mhz, 5=/1024=30Hz
#define SENSE_INTERVALS 122

#define CORRRECTION_FACTOR_SENSE_A 3
#define CORRRECTION_FACTOR_SENSE_B 3


#define MOT_CMD_SPEED 0x10 // send CMD_MOT_SPEED | MAxx | MBxx, speedA, speedB
#define MOT_CMD_SENSE 0x20 // send. receive batSense
//#define MOT_CMD_STANDBY 0x40 // send
//#define MOT_CMD_ACTIVATE 0x80 // send after standby to reactivate


#define MOTOR_DDR DDRA
#define MOTOR_PORT PORTA
#define MOTOR_PIN PINA
#define CTRL_DDR DDRB
#define CTRL_PORT PORTB
 
#define MOTOR_IN1_A  PA0
#define MOTOR_IN2_A  PA1
#define MOTOR_IN1_B  PA2
#define MOTOR_IN2_B  PA3
#define BTN_PIN      PA4 // digital input
#define BAT_SENSE_PIN PA5 // analog input
#define BAT_SENSE_ADC 0x04 // analog input ADC4 on PA5
#define MOTOR_SENS_A PA6
#define MOTOR_SENS_B PA7

#define MOTOR_EN_A   PB1
#define MOTOR_EN_B   PB3
#define MDEBUG1      PB4
#define MDEBUG2      PB5
#define MOTOR_SBY    PB6


//void speedMotorA(uint8_t speed);
//void speedMotorB(uint8_t speed);
//void dirMotors(uint8_t dir);

#define motorStandby() { CTRL_PORT &= ~(1 << MOTOR_SBY); }
#define motorActivate() { CTRL_PORT |= (1 << MOTOR_SBY); }

//#define CTRL_DEBUG_1() { CTRL_PORT = (CTRL_PORT & ~((1<<MDEBUG1)|(1<<MDEBUG2)) ) | ((1<<MDEBUG1)|(0<<MDEBUG2)); }
//#define CTRL_DEBUG_2() { CTRL_PORT = (CTRL_PORT & ~((1<<MDEBUG1)|(1<<MDEBUG2)) ) | ((0<<MDEBUG1)|(1<<MDEBUG2)); }

#define CTRL_DEBUG_1_OFF()  {CTRL_PORT &= ~(1<<MDEBUG1); }
#define CTRL_DEBUG_1_ON() {CTRL_PORT |=  (1<<MDEBUG1); }
#define CTRL_DEBUG_2_OFF()  {CTRL_PORT &= ~(1<<MDEBUG2); }
#define CTRL_DEBUG_2_ON() {CTRL_PORT |=  (1<<MDEBUG2); }




extern volatile uint8_t senseMotorA;
extern volatile uint8_t senseMotorB;

//extern volatile uint8_t adc_result;

//o//extern volatile uint8_t spdMotA;
//o//extern volatile uint8_t spdMotB;
#define spdMotA OCR1A 
#define spdMotB OCR1B 

void motor_init(void);


#endif  // ifndef _MOT_DRV_H_